﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAP.Middleware.Connector;
using CM.DL.DALC.NC03;
using DECOR.BL.BE;

namespace DECOR.DL.DALC.NC03
{
    public class CondicionDALC : ICondicionDALC
    {
        private string _connId = string.Empty;

        public static CondicionDALC Instance(string pConnId)
        {
            if (_instance == null)
            {
                lock (typeof(CondicionDALC))
                {
                    if (_instance == null)
                    {
                        _instance = new CondicionDALC(pConnId);
                    }
                }
            }
            return _instance;
        }
        private CondicionDALC(string pConnId) { this._connId = pConnId; }
        private CondicionDALC() { }
        private static volatile CondicionDALC _instance = null;

        #region ICondicionDALC Members

        public List<DECOR.BL.BE.GenericBE> listaCondicionesExpedicion()
        {
            IRfcFunction function = null; RfcDestination dest = null;
            //InMemoryDestinationConfiguration dc = new InMemoryDestinationConfiguration();
            List<GenericBE> lst = new List<GenericBE>();
            GenericBE obj = null;
            DestConfig dc = new DestConfig();

            try
            {
                //try
                //{
                //    RfcDestinationManager.RegisterDestinationConfiguration(dc);
                //}
                //catch { }

                //RfcDestination dest = RfcDestinationManager.GetDestination(this._connId);                
                dest = DALC.getDestination(out dc);
                function = dest.Repository.CreateFunction("ZWEB_LISTA_CONDICIONES");
                function.Invoke(dest);

                IRfcTable T_TVSBT = function.GetTable("T_TVSBT");
                IRfcTable T_TVKGGT = function.GetTable("T_TVKGGT");                

                foreach (IRfcStructure item in T_TVSBT)
                {
                    obj = new GenericBE();
                    obj.Codigo = item.GetString("VSBED");
                    obj.Descripcion = item.GetString("VSBED") + " :: " + item.GetString("VTEXT");
                    lst.Add(obj);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                //RfcDestinationManager.UnregisterDestinationConfiguration(dc);
            }
            return lst;
        }

        public List<DECOR.BL.BE.CondicionBE> ObtenerCondiciones(DECOR.BL.BE.PedidoBE objPedido, 
                DECOR.BL.BE.DetalleBE objDetalle, out string retval)
        {
            IRfcFunction function = null; RfcDestination dest = null;
            //InMemoryDestinationConfiguration dc = new InMemoryDestinationConfiguration();
            DestConfig dc = new DestConfig();
            List<CondicionBE> lst = new List<CondicionBE>();
            CondicionBE obj = null; 
            
            try
            {
                //try
                //{
                //    RfcDestinationManager.RegisterDestinationConfiguration(dc);
                //}
                //catch { }

                //RfcDestination dest = RfcDestinationManager.GetDestination(this._connId);
                dest = DALC.getDestination(out dc);
                RfcStructureMetadata sm = dest.Repository.GetStructureMetadata("ZSSD_MATERIAL");
                IRfcStructure PI_SMATERIAL = sm.CreateStructure();
                PI_SMATERIAL.SetValue("AUART", objPedido.CodTipoDoc);
                PI_SMATERIAL.SetValue("KUNNR", objPedido.CodCliente);
                PI_SMATERIAL.SetValue("SPART", objPedido.Sector);
                PI_SMATERIAL.SetValue("VKORG", objPedido.OrgVentas);
                PI_SMATERIAL.SetValue("VTWEG", objPedido.CanalDist);
                PI_SMATERIAL.SetValue("MATNR", objDetalle.CodMaterial);
                PI_SMATERIAL.SetValue("FECHA", ParametroDALC.dateNetToSap(objPedido.Fecha));
                PI_SMATERIAL.SetValue("CHARG", objDetalle.CodLote);
                PI_SMATERIAL.SetValue("ZTERM", objPedido.CondPago);
                PI_SMATERIAL.SetValue("KDKGR", objPedido.GrupoCond);
                PI_SMATERIAL.SetValue("KDGRP", "");
                PI_SMATERIAL.SetValue("VKBUR", objPedido.CodOficina);

                function = dest.Repository.CreateFunction("ZWEB_OBTENER_CONDICIONES");
                function.SetValue("PI_SMATERIAL", PI_SMATERIAL);
                function.Invoke(dest);

                IRfcTable STPO = function.GetTable("PE_TCONDICIONDCTO");

                foreach (IRfcStructure item in STPO)
                {
                    obj = new CondicionBE();
                    obj.ClaseCondicion = item["KSCHL"].GetString();
                    obj.Tratamiento = item["KMANU"].GetString();
                    obj.Denominacion = item["VTEXT"].GetString();
                    obj.LimiteInferior = Convert.ToDecimal(item["MXWRT"].GetString());
                    lst.Add(obj);
                }

                retval = function.GetString("PE_ERROR");
            }
            catch (RfcCommunicationException ex)
            {
                retval = "Confirme nuevamente la operación por favor.";
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                //RfcDestinationManager.UnregisterDestinationConfiguration(dc);
            }
            
            return lst;
        }

        #endregion
    }
}
